package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author majun
 * @package sky-take-out
 * @date 2024/5/31 : 上午8:18
 */

@RestController("userAddressBookController")
@RequestMapping("/user/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    private Result add(@RequestBody AddressBook addressBook) {

        log.info("新增地址：{}", addressBook);

        addressBookService.add(addressBook);

        return Result.success();

    }

    /**
     * 查询当前登录用户的所有地址信息
     *
     * @return
     */
    @GetMapping("/list")
    private Result<List<AddressBook>> list() {

        log.info("查询当前登录用户的所有地址信息");

        List<AddressBook> list = addressBookService.list();

        return Result.success(list);

    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("/default")
    private Result<AddressBook> def() {

        log.info("查询默认地址");

        AddressBook list = addressBookService.def();

        return Result.success(list);

    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    private Result<AddressBook> getFromById(@PathVariable Long id) {

        log.info("根据id查询地址：{}", id);

        AddressBook list = addressBookService.getFromById(id);

        return Result.success(list);

    }

    /**
     * 根据id修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    private Result put(@RequestBody AddressBook addressBook) {

        log.info("根据id修改地址:{}", addressBook);

        addressBookService.put(addressBook);

        return Result.success();

    }

    /**
     * 根据id删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping
    private Result delete(Long id) {

        log.info("根据id删除地址:{}", id);

        addressBookService.delete(id);

        return null;

    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    private Result putDef(@RequestBody AddressBook addressBook) {

        log.info("设置默认地址:{}", addressBook);

        addressBookService.putDef(addressBook);

        return Result.success();

    }

}
